<?php
namespace App\Libraries\Decorator\Balance;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Logo;
use App\Libraries\Tools\Msg;
use App\Libraries\Tools\URL;
use App\Models\Shop\Shop;
use App\Models\Shop\ShopCouponPurchase;
use App\Models\User\User;

class BalanceDecorator implements ModelDecorator
{

    private $id;
    private $type;

    public function __construct($id, $type){
        $this->id = $id;
        $this->type = $type;
    }

    public function before(){
        //todo
    }

    public function after(&$data){
        if(!is_array($data['list'])){
            $data = json_decode(json_encode($data), true);
        }
        $rs = [];
        foreach($data['list'] as $v){
            if($v['refund']){
                $rs[] = $this->refundHandler($v);
                continue;
            }
            switch($v['origin_type']){
                case ConstVar::$ORDER_TYPE_POST:
                    $rs[] = $this->postHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_PRODUCT:
                    $rs[] = $this->productHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_TRANSFER_TO_SHOP:
                    $rs[] = $this->transferToShopHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_COUPON:
                    $rs[] = $this->couponHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_TRANSFER_TO_USER:
                    $rs[] = $this->transferToUserHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_HONGBAO:
                    $rs[] = $this->ErrorHandler($v);
                    break;
                case ConstVar::$ORDER_TYPE_WITHDRAW:
                    $rs[] = $this->withdrawHandler($v);
                    break;
                default:
                    $rs[] = $this->ErrorHandler($v);
                    break;
            }
        }
        $data['list'] = $rs;
        return $data;
    }

    private function postHandler($data){
        $rs = [];
        if($data['from_id'] == $this->id){
            $rs['title'] = '购买主题';
            $rs['logo'] = Logo::$POST;
            $rs['balance'] = '-'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }else{
            $rs['title'] = '主题收益';
            $rs['logo'] = Logo::$POST;
            $rs['balance'] = '+'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }
        return $rs;
    }

    private function productHandler($data){
        $rs = [];
        if($data['from_id'] == $this->id){
            $rs['title'] = '购买商品';
            $rs['logo'] = Logo::$SHOPPING;
            $rs['balance'] = '-'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }else{
            $rs['title'] = '错误数据';
            $rs['logo'] = Logo::$ERROR;
            $rs['balance'] = $data['balance'];
            $rs['created_at'] = $data['created_at'];
        }
        return $rs;
    }

    private function couponHandler($data){
        $rs = [];
        if($data['from_id'] == $this->id){
            $rs['title'] = '购买套餐';
            $rs['logo'] = Logo::$SHOPPING;
            $rs['balance'] = '-'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }else{
            $purchase = ShopCouponPurchase::where('id', $data['purchase_id'])->first();
            $rs['title'] = '套餐收益-'.$purchase->title;
            $rs['logo'] = URL::getImage($purchase->cover);
            $rs['balance'] = '+'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }
        return $rs;
    }

    private function transferToShopHandler($data){
        $rs = [];
        if($data['from_id'] == $this->id){
            $shop = Shop::where('shop_id', $data['origin_id'])->first();
            if(empty($shop)){
                $rs['title'] = '扫码支付';
            }else{
                $rs['title'] = '扫码支付-支付给'.$shop->title;
            }
            $rs['logo'] = Logo::$BUSINESS;
            $rs['balance'] = '-'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }else{
            $user = User::where('user_id', $data['from_id'])->first();
            if(empty($user)){
                $rs['title'] = '扫码收款';
                $rs['logo'] = '';
            }else{
                $rs['title'] = '扫码收款-'.$user->nickname;
                $rs['logo'] = URL::getImage($user->cover);
            }
            $rs['balance'] = '+'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }
        return $rs;
    }

    private function transferToUserHandler($data){
        $rs = [];
        if($data['from_id'] == $this->id){
            $user = User::where('user_id', $data['to_id'])->first();
            if(empty($user)){
                $rs['title'] = '转账支付';
                $rs['logo'] = '';
            }else{
                $rs['title'] = '转账支付-'.$user->nickname;
                $rs['logo'] = URL::getImage($user->cover);
            }
            $rs['balance'] = '-'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }else{
            $user = User::where('user_id', $data['from_id'])->first();
            if(empty($user)){
                $rs['title'] = '转账收款';
                $rs['logo'] = '';
            }else{
                $rs['title'] = '转账收款-'.$user->nickname;
                $rs['logo'] = URL::getImage($user->cover);
            }
            $rs['balance'] = '+'.$data['balance'];
            $rs['created_at'] = $data['created_at'];
        }
        return $rs;
    }

    private function withdrawHandler($data){
        $rs = [];
        $rs['title'] = '提现';
        $rs['logo'] = Logo::$BALANCE;
        $rs['balance'] = '-'.$data['balance'];
        $rs['created_at'] = $data['created_at'];
        return $rs;
    }

    private function refundHandler($data){
        switch($data['to_channel']){
            case 1:
                $to_channel = '到支付宝';
                $logo = Logo::$ALIPAY;
                break;
            case 2:
                $to_channel = '到微信';
                $logo = Logo::$WECHAT;
                break;
            case 3:
                $to_channel = '到苹果账户';
                $logo = Logo::$APPLE;
                break;
            case 4:
                $to_channel = '到余额';
                $logo = Logo::$BALANCE;
                break;
            default:
                $to_channel = '到余额';
                $logo = Logo::$BALANCE;
                break;
        }
        $rs['title'] = '退款-'.$to_channel;
        $rs['logo'] = $logo;
        $rs['balance'] = '+'.$data['balance'];
        $rs['created_at'] = $data['created_at'];
        return $rs;
    }

    private function ErrorHandler($data){
        $rs['title'] = '错误数据';
        $rs['logo'] = Logo::$ERROR;
        $rs['balance'] = $data['balance'];
        $rs['created_at'] = $data['created_at'];
        return $rs;
    }
}